home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
L' Effet Pommier 3
/
L'Effet Pommier - Volume 03.iso
/
Programmation
/
gray image 2.1
/
display.cc
< prev
next >
Wrap
Text File
|
1995-06-13
|
2KB
|
68 lines
/*
***********************************************************************
*
*
* Grayscale Image
* Display an image on Macintosh
*
* The program converts an image to a pixmap in an offscreen Grafport
* and has it displayed in a window
*
* We take advantage of the fact the image is traversed row-by-row
* by a PixelPrimAction iterator
*
***********************************************************************
*/
#include "image.h"
#include "window.h"
void IMAGE::display(const char * title) const
{
class ImageWindow : public OffScreenWindow
{
public:
ImageWindow(IMAGE& image, const char * title)
: OffScreenWindow((ScreenRect)image,title,40) // 40 is Gray CLUT id
{
// Move pixels from IMAGE to a off-screen pixmap
// Note that the rows of pixmap may be padded, by
// as much as off_to_next_row bytes. So we need
// to skip over the padding as we move pixels row
// by row
class ImageToPixMap : public PixelPrimAction
{
PixMapHandle pixmap;
char * pixp;
const card height, width, off_to_next_row;
card curr_row, curr_col;
void operation(GRAY& pixel) // put a pixel to pixmap[row,col]
{
*pixp++ = pixel;
if( ++curr_col >= width )
curr_col = 0, curr_row++, pixp += off_to_next_row;
}
public:
ImageToPixMap(OffScreenWindow& window)
: pixmap(window.get_pixmap()),
height(window.height()),
width(window.width()),
off_to_next_row(window.bytes_per_row()-window.width()),
curr_row(0), curr_col(0)
{
assert( LockPixels(pixmap) );
pixp = GetPixBaseAddr(pixmap);
}
~ImageToPixMap(void)
{
assert( curr_row == height && curr_col == 0 );
UnlockPixels(pixmap); pixmap = 0;
}
};
image.apply(ImageToPixMap(*this));
}
};
ImageWindow image_window(*this,title);
image_window.handle();
}